/**
 * @filename IndexController.java
 */
package com.liupf.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.simple.JSONArray;

import com.jfinal.core.Controller;
import com.liupf.common.utils.Constants;
import com.liupf.domain.Driver;

/**
 * @description 首页
 * @author shevliu
 * @email shevliu@gmail.com
 * Jul 26, 2012 9:32:14 PM
 */
public class MapController extends Controller{

	private Log log = LogFactory.getLog(this.getClass()) ;
	
	public void index(){
		String company=(String) getSession().getAttribute("company");
		String userRole = (String)getSession().getAttribute("userRole");
		String syscity = (String) getSession().getAttribute("syscity");
		String job_number = getPara("job_number");
		String sql = "select * from b_user where latitude is not null and longitude is not null and canuse=1" ;
		if(StringUtils.isNotBlank(job_number)){
			sql += " and job_number='"+job_number+"' ";
		}
		if(Constants.ROLE_BRAND_MANAGER.equals(userRole) || Constants.ROLE_MIDDLE_MANAGER.equals(userRole)){
			String companySql = "select company from tel_company where city='"+syscity+"'";
			//sql += " and license_num='"+company+"'";
			sql += " and license_num in ("+companySql+")";
		}
		List<Driver> list = Driver.dao.find(sql);
		List<Map<String,Object>> jsonList = new ArrayList<Map<String,Object>>();
		
		Map<String,Integer> statusCountMap = new HashMap<String, Integer>();
		
		for(Driver driver : list){
			Map<String,Object> paramsMap = new HashMap<String, Object>();
			paramsMap.put("longitude", driver.getBigDecimal("longitude"));
			paramsMap.put("latitude", driver.getBigDecimal("latitude"));
			
			paramsMap.put("job_number", driver.getStr("job_number") == null ? "":driver.getStr("job_number"));
			paramsMap.put("head", driver.getStr("head") == null ? "":driver.getStr("head"));
			
			paramsMap.put("driver_license", driver.getStr("driver_license") == null ? "":driver.getStr("driver_license"));
			paramsMap.put("sex", driver.getInt("sex"));
			
			int status = driver.getInt("status");
			paramsMap.put("status", status);
			
			String name = driver.getStr("name");
			paramsMap.put("name", name);
			
			String phone = driver.getStr("phone");
			paramsMap.put("phone", phone);
			
			String license_num = driver.getStr("license_num");
			paramsMap.put("license_num", license_num);
			
			Integer count = statusCountMap.get(status+"");
			if(count == null){
				statusCountMap.put(status+"", 1);
			}else{
				statusCountMap.put(status+"", ++count);
			}
			
			jsonList.add(paramsMap);
		}
		setAttr("list", list);
		String jsonStr = JSONArray.toJSONString(jsonList);
		setAttr("jsonStr", jsonStr);
		//冻结的 单独处理下 
		sql="SELECT *  FROM b_user WHERE  (canuse=1 OR ( latitude IS NULL AND longitude IS NULL))";
		List<Driver> free=Driver.dao.find(sql);
		statusCountMap.put("99", free.size());
		setAttr("statusCountMap", statusCountMap);
		System.out.println("statusCountMap="+statusCountMap);
		render("driver_all.ftl");
	}
}
